package com.tang.controller;

import com.tang.entity.User;
import com.tang.service.UserService;
import com.tang.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author TangBowen
 * @create 2021/6/7 12:52
 */
@WebServlet("/userServlet")
public class UserController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取参数并且封装
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username).setPassword(password);

        //调用业务层登陆方法
        List<User> users = userService.login(user);

        //判断是否查询出结果
        if (users.size()!=0){
            //将用户名存入会话域
            req.getSession().setAttribute("username",username);
            resp.getWriter().write("true");
        }else {
            resp.getWriter().write("false");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
